

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.text.NumberFormat;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.renderer.xy.XYDotRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.function.NormalDistributionFunction2D;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;

public class GraphPlotter extends ApplicationFrame {

    private static final long serialVersionUID = 1L;
    private static int[] data;
    private static int[] data2;
    private static int[] data3;
    private static int[] data4;
    private static int[] data5;
    private static int[] data6;
    private static int[] data7;
    private static int[] data8;
    
    public GraphPlotter(String s) {
     	
        super(s);
        
        data = GraphMiningPresenter.getInformation(System.getProperty("user.dir") + "/data/Session2/P cox2 80.txt");
        data2 = GraphMiningPresenter.getInformation(System.getProperty("user.dir") + "/data/Session2/M 5 cox2 80.txt");
        data3 = GraphMiningPresenter.getInformation(System.getProperty("user.dir") + "/data/Session2/T cox2 80.txt");
        
        data4 = GraphMiningPresenter.getInformation(System.getProperty("user.dir") + "/data/Session2/P cpdb 80.txt");
        data5 = GraphMiningPresenter.getInformation(System.getProperty("user.dir") + "/data/Session2/M 5 cpdb 80.txt");
        data6 = GraphMiningPresenter.getInformation(System.getProperty("user.dir") + "/data/Session2/T cpdb 80.txt");
        
//        data = GraphMiningPresenter.getInformation(System.getProperty("user.dir") + "/data/Session2/P cox2 30.txt");
//        data2 = GraphMiningPresenter.getInformation(System.getProperty("user.dir") + "/data/Session2/M 5 cox2 30.txt");
//        data3 = GraphMiningPresenter.getInformation(System.getProperty("user.dir") + "/data/Session2/T cox2 30.txt");
//        
//        data4 = GraphMiningPresenter.getInformation(System.getProperty("user.dir") + "/data/Session2/P cpdb 30.txt");
//        data5 = GraphMiningPresenter.getInformation(System.getProperty("user.dir") + "/data/Session2/M 5 cpdb 30.txt");
//        data6 = GraphMiningPresenter.getInformation(System.getProperty("user.dir") + "/data/Session2/T cpdb 30.txt");
        
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(jpanel);
    }

    public static JPanel createDemoPanel() {
        JFreeChart jfreechart = createChart();
        ChartPanel chartpanel = new ChartPanel(jfreechart);
        chartpanel.setMouseWheelEnabled(true);
        return chartpanel;
    }
    
    /**
     * Returns a sample dataset.
     * 
     * @return The dataset.
     */
    private static CategoryDataset createDataset() {
        
        // row keys...
        final String series1 = "Cox2 p s-count: 80";
        final String series2 = "Cox2 m 5 s-count: 80";
        final String series3 = "Cox2 t s-count: 80";
        
        final String series4 = "CPDB p s-count: 80";
        final String series5 = "CPDB m 5 s-count: 80";
        final String series6 = "CPDB t s-count: 80";
    	
//        final String series1 = "Cox2 p s-count: 30";
//        final String series2 = "Cox2 m 5 s-count: 30";
//        final String series3 = "Cox2 t s-count: 30";
//        
//        final String series4 = "CPDB p s-count: 30";
//        final String series5 = "CPDB m 5 s-count: 30";
//        final String series6 = "CPDB t s-count: 30";

        // column keys...
        final String category1 = "min Vertices";
        final String category2 = "max Vertices";
        final String category3 = "Average Vertices";
        final String category4 = "min Edges";
        final String category5 = "max Edges";
        final String category6 = "Average Edges";

        // create the dataset...
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(data[0], series1, category1);
        dataset.addValue(data[1], series1, category2);
        dataset.addValue(data[2], series1, category3);
        dataset.addValue(data[3], series1, category4);
        dataset.addValue(data[4], series1, category5);
        dataset.addValue(data[5], series1, category6);
        
        dataset.addValue(data2[0], series2, category1);
        dataset.addValue(data2[1], series2, category2);
        dataset.addValue(data2[2], series2, category3);
        dataset.addValue(data2[3], series2, category4);
        dataset.addValue(data2[4], series2, category5);
        dataset.addValue(data2[5], series2, category6);
        
        dataset.addValue(data3[0], series3, category1);
        dataset.addValue(data3[1], series3, category2);
        dataset.addValue(data3[2], series3, category3);
        dataset.addValue(data3[3], series3, category4);
        dataset.addValue(data3[4], series3, category5);
        dataset.addValue(data3[5], series3, category6);
        
        dataset.addValue(data4[0], series4, category1);
        dataset.addValue(data4[1], series4, category2);
        dataset.addValue(data4[2], series4, category3);
        dataset.addValue(data4[3], series4, category4);
        dataset.addValue(data4[4], series4, category5);
        dataset.addValue(data4[5], series4, category6);
        
        dataset.addValue(data5[0], series5, category1);
        dataset.addValue(data5[1], series5, category2);
        dataset.addValue(data5[2], series5, category3);
        dataset.addValue(data5[3], series5, category4);
        dataset.addValue(data5[4], series5, category5);
        dataset.addValue(data5[5], series5, category6);
        
        dataset.addValue(data6[0], series6, category1);
        dataset.addValue(data6[1], series6, category2);
        dataset.addValue(data6[2], series6, category3);
        dataset.addValue(data6[3], series6, category4);
        dataset.addValue(data6[4], series6, category5);
        dataset.addValue(data6[5], series6, category6);
        
//        dataset.addValue(data7[0], series7, category1);
//        dataset.addValue(data7[1], series7, category2);
//        dataset.addValue(data7[2], series7, category3);
//        dataset.addValue(data7[3], series7, category4);
//        dataset.addValue(data7[4], series7, category5);
//        dataset.addValue(data7[5], series7, category6);
//        
//        dataset.addValue(data8[0], series8, category1);
//        dataset.addValue(data8[1], series8, category2);
//        dataset.addValue(data8[2], series8, category3);
//        dataset.addValue(data8[3], series8, category4);
//        dataset.addValue(data8[4], series8, category5);
//        dataset.addValue(data8[5], series8, category6);
        
        return dataset;
        
    }
    


    public static JFreeChart createChart() 
    {
        JFreeChart jfreechart = ChartFactory.createBarChart("Graph Mining gSpan data", "Category", "Value", createDataset()
        		, PlotOrientation.VERTICAL, true, true, false);
        
        jfreechart.setBackgroundPaint(Color.WHITE);
        
        // get a reference to the plot for further customisation...
        final CategoryPlot plot = jfreechart.getCategoryPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        // set the range axis to display integers only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // disable bar outlines...
        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setBarPainter(new StandardBarPainter());
        renderer.setDrawBarOutline(false);
        
        // set up gradient paints for series...
        final GradientPaint gp0 = new GradientPaint(
            0.0f, 0.0f, Color.blue, 
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp1 = new GradientPaint(
            0.0f, 0.0f, Color.green, 
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp2 = new GradientPaint(
            0.0f, 0.0f, Color.red, 
            0.0f, 0.0f, Color.lightGray
        );
        renderer.setSeriesPaint(0, gp0);
        
        final CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(
            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));
        
        TextTitle legendText = new TextTitle("p means: Restrict search to linear fragments\n "
        		+ "t means: Restrict search to acyclic fragments\n m means: Restrict search to fragments with max i adges");
        legendText.setPosition(RectangleEdge.BOTTOM);
        jfreechart.addSubtitle(legendText);

        return jfreechart;
    }

    public static void main(String args[]) {
    	GraphPlotter normaldistributiondemo2 = new GraphPlotter("JFreeChart: NormalDistributionDemo2.java");
        normaldistributiondemo2.pack();
        RefineryUtilities.centerFrameOnScreen(normaldistributiondemo2);
        normaldistributiondemo2.setVisible(true);
    }
}